package main

import (
	"fmt"
	"time"
)

func main() {
	c := make(chan int, 3) //带有缓冲的channel

	// channel有两个属性：len(), cap()
	fmt.Println("len(c) = ", len(c), ", cap(c)", cap(c))

	go func() {
		defer fmt.Println("子go程结束")
		for i := 0; i < 4; i++ {
			c <- i
			fmt.Printf("子go程正在运行, 发送的元素 = %d, "+
				"len(c) = %d cap(c) = %d\n", i, len(c), cap(c))
		}
	}()

	time.Sleep(2 * time.Second)
	for i := 0; i < 4; i++ {
		num := <-c //从c中接收数据，并赋值给num
		fmt.Printf("main程正在运行，接受的元素= %d,"+
			"len(c) = %d, cap(c) = %d\n", num, len(c), cap(c))
	}
	fmt.Println("main 结束")
}
